<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <!-- <el-form-item label="客户编号" prop="customerId">
        <el-input
          v-model="queryParams.customerId"
          placeholder="请输入客户编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
      <el-form-item label="企业名称" prop="customerName">
        <el-input
          v-model="queryParams.customerName"
          placeholder="请输入企业名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="统一社会信用代码" prop="uniscid" label-width="140px">
        <el-input
          v-model="queryParams.uniscid"
          placeholder="请输入统一社会信用代码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="上传机构" prop="deptId">
        <el-select v-model="queryParams.deptId" placeholder="请选择上传机构" class="input_info">
          <el-option v-for="item in organ" :key="item.key" :label="item.value" :value="item.key"></el-option>
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="业务所属地区代码" prop="areaCode">
        <el-input
          v-model="queryParams.areaCode"
          placeholder="请输入业务所属地区代码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="授信合同号" prop="approveContractNo">
        <el-input
          v-model="queryParams.approveContractNo"
          placeholder="请输入授信合同号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="授信金额" prop="approveAmount">
        <el-input
          v-model="queryParams.approveAmount"
          placeholder="请输入授信金额"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="授信开始日期(例:2020/10/1)" prop="approveStart">
        <el-input
          v-model="queryParams.approveStart"
          placeholder="请输入授信开始日期(例:2020/10/1)"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="授信到期日期(例:2020/10/1)" prop="approveEnd">
        <el-input
          v-model="queryParams.approveEnd"
          placeholder="请输入授信到期日期(例:2020/10/1)"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="借款合同号" prop="loanContractNo">
        <el-input
          v-model="queryParams.loanContractNo"
          placeholder="请输入借款合同号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="借款金额(万元)" prop="loanAmount">
        <el-input
          v-model="queryParams.loanAmount"
          placeholder="请输入借款金额(万元)"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="借款日期(例:2020/10/1)" prop="loanStart">
        <el-input
          v-model="queryParams.loanStart"
          placeholder="请输入借款日期(例:2020/10/1)"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="贷款期限(月)" prop="loanTime">
        <el-input
          v-model="queryParams.loanTime"
          placeholder="请输入贷款期限(月)"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="贷款利率(%)" prop="loanRatio">
        <el-input
          v-model="queryParams.loanRatio"
          placeholder="请输入贷款利率(%)"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="担保方式" prop="guaranteeType">
        <el-select v-model="queryParams.guaranteeType" placeholder="请选择担保方式" clearable size="small">
          <el-option
            v-for="dict in guaranteeTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="担保方式名称" prop="guaranteeTypeCn">
        <el-input
          v-model="queryParams.guaranteeTypeCn"
          placeholder="请输入担保方式名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否首贷" prop="ifFirstLoan">
        <el-input
          v-model="queryParams.ifFirstLoan"
          placeholder="请输入是否首贷"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="info" icon="el-icon-upload2" size="mini" @click="handleImport">导入</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" icon="el-icon-download" size="mini" @click="handleExport">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="uploadList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="企业名称" align="center" prop="customerName" />
      <el-table-column label="统一社会信用代码" align="center" prop="uniscid" />
      <el-table-column label="上传机构 " align="center" prop="createDept" />
      <el-table-column label="是否实名" align="center" prop="ifAuth" :formatter="ifAuthFormat" />
      <el-table-column label="是否预警" align="center" prop="ifExecWarn" :formatter="ifExecWarnFormat" />
      <el-table-column label="区域" align="center" prop="areaCode" :formatter="areaCodeFormat" />
      <el-table-column label="授信合同号" align="center" prop="approveContractNo" />
      <el-table-column label="授信金额(万元)" align="center" prop="approveAmount" />
      <el-table-column label="授信开始日期" align="center" prop="approveStart" />
      <el-table-column label="授信到期日期" align="center" prop="approveEnd" />
      <el-table-column label="借款合同号" align="center" prop="loanContractNo" />
      <el-table-column label="借款金额(万元)" align="center" prop="loanAmount" />
      <el-table-column label="借款日期" align="center" prop="loanStart" />
      <el-table-column label="借款期限(月)" align="center" prop="loanTime" />
      <el-table-column label="借款利率(%)" align="center" prop="loanRatio" />
      <el-table-column
        label="担保方式"
        align="center"
        prop="guaranteeType"
        :formatter="guaranteeTypeFormat"
      />
      <el-table-column
        label="是否首贷"
        align="center"
        prop="ifFirstLoan"
        :formatter="ifFirstLoanFormat"
      />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)">修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改存量业务对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body class="addLoan">
      <el-form ref="form" :model="form" :rules="rules" label-width="160px">
        <!-- <el-form-item label="客户编号" prop="customerId">
          <el-input v-model="form.customerId" placeholder="请输入客户编号" />
        </el-form-item>-->
        <el-form-item label="企业名称" prop="customerName">
          <el-input v-model="form.customerName" placeholder="请输入客户名称" />
        </el-form-item>
        <el-form-item label="统一社会信用代码" prop="uniscid">
          <el-input v-model="form.uniscid" placeholder="请输入统一社会信用代码" />
        </el-form-item>
        <el-form-item label="业务所属地区代码" prop="areaCode">
          <el-select v-model="form.areaCode" placeholder="请选择业务所属地区">
            <el-option
              v-for="dict in areaCodeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="授信合同号" prop="approveContractNo">
          <el-input v-model="form.approveContractNo" placeholder="请输入授信合同号" />
        </el-form-item>
        <el-form-item label="授信金额(万元)" prop="approveAmount">
          <el-input v-model="form.approveAmount" placeholder="请输入授信金额" />
        </el-form-item>
        <el-form-item label="授信开始日期 " prop="approveStart">
          <el-date-picker
            value-format="yyyy-MM-dd"
            v-model="form.approveStart"
            type="date"
            placeholder="请选择授信开始日期"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="授信到期日期" prop="approveEnd">
          <el-date-picker
            value-format="yyyy-MM-dd"
            v-model="form.approveEnd"
            type="date"
            placeholder="请选择授信到期日期"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="借款合同号" prop="loanContractNo">
          <el-input v-model="form.loanContractNo" placeholder="请输入借款合同号" />
        </el-form-item>
        <el-form-item label="借款金额(万元)" prop="loanAmount">
          <el-input v-model="form.loanAmount" placeholder="请输入借款金额(万元)" />
        </el-form-item>
        <el-form-item label="借款日期" prop="loanStart">
          <el-date-picker
            value-format="yyyy-MM-dd"
            v-model="form.loanStart"
            type="date"
            placeholder="请选择借款日期"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="贷款期限(月)" prop="loanTime">
          <el-input v-model="form.loanTime" placeholder="请输入贷款期限(月)" />
        </el-form-item>
        <el-form-item label="贷款利率(%)" prop="loanRatio">
          <el-input v-model="form.loanRatio" placeholder="请输入贷款利率(%)" />
        </el-form-item>
        <el-form-item label="担保方式" prop="guaranteeType">
          <el-select v-model="form.guaranteeType" placeholder="请选择担保方式">
            <el-option
              v-for="dict in guaranteeTypeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="担保方式名称" prop="guaranteeTypeCn">
          <el-input v-model="form.guaranteeTypeCn" placeholder="请输入担保方式名称" />
        </el-form-item>-->
        <el-form-item label="是否首贷" prop="ifFirstLoan">
          <el-select v-model="form.ifFirstLoan" placeholder="请选择是否首贷">
            <el-option
              v-for="dict in ifFirstLoanOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 用户导入对话框 -->
    <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
      <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="upload.url + '?updateSupport=' + upload.updateSupport"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">
          将文件拖到此处，或
          <em>点击上传</em>
        </div>
        <div class="el-upload__tip" slot="tip" style="text-align:right">
          <!-- <el-checkbox v-model="upload.updateSupport" />是否更新已经存在的用户数据 -->
          <el-link type="info" class="downloadInfo" @click="importTemplate">下载模板</el-link>
        </div>
        <div class="el-upload__tip" style="color:red" slot="tip">提示：仅允许导入“xls”或“xlsx”格式文件！</div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listUpload,
  getUpload,
  delUpload,
  addUpload,
  updateUpload,
  importTemplate,
  exportinventory
} from "@/api/inventory/upload";
import { listDept } from "@/api/system/dept";
import { getToken } from "@/utils/auth";
export default {
  name: "Upload",
  components: {},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 存量业务表格数据
      uploadList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 业务所属地区代码字典
      areaCodeOptions: [],
      // 担保方式字典
      guaranteeTypeOptions: [],
      // 是否首贷字典
      ifFirstLoanOptions: [],
      // 是否实名字典
      ifAuthOptions: [],
      // 是否预警字典
      ifExecWarnOptions: [],
      // 用户导入参数
      upload: {
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url:
          process.env.VUE_APP_BASE_API + "/afterloan/inventory-loan/importData"
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        customerId: null,
        customerName: null,
        uniscid: null,
        areaCode: null,
        approveContractNo: null,
        approveAmount: null,
        approveStart: null,
        approveEnd: null,
        loanContractNo: null,
        loanAmount: null,
        loanStart: null,
        loanTime: null,
        loanRatio: null,
        guaranteeType: null,
        guaranteeTypeCn: null,
        ifFirstLoan: null,
        deptId: null
      },
      organ: [],

      // 表单参数
      form: {},
      // 表单校验
      rules: {
        customerId: [
          { required: true, message: "客户编号不能为空", trigger: "blur" }
        ],
        customerName: [
          { required: true, message: "客户名称不能为空", trigger: "blur" }
        ],
        uniscid: [
          {
            required: true,
            message: "统一社会信用代码不能为空",
            trigger: "blur"
          }
        ],
        areaCode: [
          {
            required: true,
            message: "业务所属地区代码不能为空",
            trigger: "blur"
          }
        ],
        approveContractNo: [
          { required: true, message: "授信合同号不能为空", trigger: "blur" }
        ],
        approveStart: [
          {
            required: true,
            message: "授信开始日期(例:2020/10/1)不能为空",
            trigger: "blur"
          }
        ],
        approveEnd: [
          {
            required: true,
            message: "授信到期日期(例:2020/10/1)不能为空",
            trigger: "blur"
          }
        ],
        approveAmount: [
          { required: true, message: "授信金额不能为空", trigger: "blur" }
        ],
        loanAmount: [
          { required: true, message: "借款金额不能为空", trigger: "blur" }
        ],
        loanContractNo: [
          { required: true, message: "借款合同号不能为空", trigger: "blur" }
        ],
        loanStart: [
          {
            required: true,
            message: "借款日期(例:2020/10/1)不能为空",
            trigger: "blur"
          }
        ],
        loanTime: [
          { required: true, message: "贷款期限(月)不能为空", trigger: "blur" }
        ],
        loanRatio: [
          { required: true, message: "贷款利率(%)不能为空", trigger: "blur" }
        ],
        guaranteeType: [
          { required: true, message: "担保方式不能为空", trigger: "change" }
        ],
        guaranteeTypeCn: [
          { required: true, message: "担保方式名称不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList2();
    this.getList();
    this.getDicts("area_codes").then(response => {
      this.areaCodeOptions = response.data;
    });
    this.getDicts("guarantee_type").then(response => {
      this.guaranteeTypeOptions = response.data;
    });
    this.getDicts("sys_yes_no").then(response => {
      this.ifFirstLoanOptions = response.data;
      this.ifAuthOptions = response.data;
      this.ifExecWarnOptions = response.data;
    });
  },
  methods: {
    /** 查询部门列表 */
    getList2() {
      this.loading = true;
      var param = {
        parentId: "13"
      };
      listDept(param).then(response => {
        response.data.forEach(element => {
          this.organ.push({ key: element.deptId, value: element.deptName });
        });
      });
    },
    /** 查询存量业务列表 */
    getList() {
      this.loading = true;
      listUpload(this.queryParams).then(response => {
        this.uploadList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 业务所属地区代码字典翻译
    areaCodeFormat(row, column) {
      return this.selectDictLabel(this.areaCodeOptions, row.areaCode);
    },
    // 担保方式字典翻译
    guaranteeTypeFormat(row, column) {
      return this.selectDictLabel(this.guaranteeTypeOptions, row.guaranteeType);
    },
    // 是否首贷字典翻译
    ifFirstLoanFormat(row, column) {
      return this.selectDictLabel(this.ifFirstLoanOptions, row.ifFirstLoan);
    },
    // 是否实名字典翻译
    ifAuthFormat(row, column) {
      return this.selectDictLabel(this.ifAuthOptions, row.ifAuth);
    },
    // 是否预警字典翻译
    ifExecWarnFormat(row, column) {
      return this.selectDictLabel(this.ifExecWarnOptions, row.ifExecWarn);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        customerId: null,
        customerName: null,
        uniscid: null,
        areaCode: null,
        approveContractNo: null,
        approveAmount: null,
        approveStart: null,
        approveEnd: null,
        loanContractNo: null,
        loanAmount: null,
        loanStart: null,
        loanTime: null,
        loanRatio: null,
        guaranteeType: null,
        guaranteeTypeCn: null,
        ifFirstLoan: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加存量业务";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getUpload(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改存量业务";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateUpload(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addUpload(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm(
        '是否确认删除存量业务编号为"' + ids + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      )
        .then(function() {
          return delUpload(ids);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        });
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有存量业务数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(function() {
          return exportinventory(queryParams);
        })
        .then(response => {
          this.download(response.msg);
        });
    },
    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = "用户导入";
      this.upload.open = true;
    },
    /** 下载模板操作 */
    importTemplate() {
      importTemplate().then(response => {
        this.download(response.msg);
      });
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert(response.msg, "导入结果", { dangerouslyUseHTMLString: true });
      this.getList();
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    }
  }
};
</script>
<style scoped>
/deep/ .el-dialog {
  display: flex;
  flex-direction: column;
  margin: 0 !important;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  max-height: calc(100% -30px);
  /* max-width: ; */
}
.addLoan /deep/ .el-dialog__body {
  height: 380px;
  overflow-y: auto;
  padding-top: 0;
}
.downloadInfo {
  font-size: 16px;
  font-weight: bold;
  letter-spacing: 2px;
}
</style>
